/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.server.dataanalysis;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.bioteam.appweb.Config;
import net.bioteam.appweb.gwt.apps.server.program.ProgramHandler;
import net.bioteam.appweb.model.Program;
import net.bioteam.appweb.model.ProgramBuilder;
import net.bioteam.appweb.submit.UnfinishedJob;
import net.bioteam.common.user.User;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Servlet implementation class ProgramHandler
 */
public class DataProgramHandler extends ProgramHandler
{
	private static final long serialVersionUID = 1L;
	public static final Log logger = LogFactory.getLog(DataProgramHandler.class);
	public static long MAX_FILE_SIZE_TO_SAVE = 1024 * 1024;

	public DataProgramHandler()
	{
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException
	{

		User user = (User) request.getSession().getAttribute("user");
		// Parse the request
		File dir = new File(Config.USER_TMP + File.separator + user.getName());
		if (!dir.exists())
		{
			dir.mkdirs();
		}
		List<File> tempFiles=new LinkedList<File>();
		try
		{
			Map<String, String> values = retrieveValue(request, dir, user, tempFiles);
			String msg = null;
			Program program=ProgramBuilder.getProgram(values.get("_program"));
			postProcessValue(values, program);

			AnalysisData data = (AnalysisData) request.getSession().getAttribute(AnalysisData.AnalysisSessionKey);
			values.put(data.paramName, data.getInputfile());
			
			UnfinishedJob runStatus = processRun(values, user, data.getMap().getFilekey());
			msg = getUploadRetMessage(true, null, "user_jobs_"+runStatus.getId());
			

			resp.setContentType("text/html");
			resp.setContentLength(msg.length());
			resp.getWriter().write(msg);
			
		} catch (Exception e)
		{
			String msg1 = e.getMessage();
			if (msg1 == null)
			{
				msg1 = "unknown server error";
			}
			String msg = getUploadRetMessage(false,msg1, null);
			resp.setContentType("text/html");
			resp.setContentLength(msg.length());
			resp.getWriter().write(msg);

		} finally
		{
			for (File file : tempFiles)
			{
				if (file.exists())
					file.delete();
			}
		}

	}

}
